package com.yc.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.vo.Result;
import com.yc.entity.Department;
import com.yc.mapper.DepartmentMapper;
import com.yc.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(value = "所属学院控制器")
@RestController
@RequestMapping("/api/udp")
public class DepartmentController {

    @Resource
    private DepartmentService udService;
    @Autowired
    private DepartmentMapper universityDepartmentMapper;

    @PostMapping("/save")
    public Result<?> save(@RequestBody Department ud) {
        return Result.success(udService.save(ud));
    }

    @GetMapping("/{id}")
    public Result<Department> findById(@PathVariable Long id) {
        return Result.success(udService.getById(id));
    }

    @GetMapping("/findAll")
    public Result<List<Department>> findAll() {
        return Result.success(udService.list());
    }

    @GetMapping("/page")
    public Result<IPage<Department>> findPage(@RequestParam(required = false, defaultValue = "") String name,
                                              @RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                              @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return Result.success(udService.page(new Page<>(pageNum, pageSize), Wrappers.<Department>lambdaQuery().like(Department::getUdname, name)));
    }

    @ApiOperation(value = "查询所有学院信息", httpMethod = "POST")
    @PostMapping("/queryAllUDInfo")
    public Result queryAllUDInfo() {
        List<Department> list= universityDepartmentMapper.queryAllUniversityDepartmentInfo();
        if(null == list || list.isEmpty()){
            return Result.error("暂无数据!");
        }
        return Result.success(list);
    }


    @ApiOperation(value = "新增学院", httpMethod = "POST")
    @RequestMapping("addDepartment")
    public Result addDepartment(String udName){
        Result re = new Result();
        Department department = new Department();
        department.setUdname(udName);
        int result=universityDepartmentMapper.insert(department);
        if(result<=0){
            return Result.error("新增学院失败!");
        }
        return Result.success("新增学院成功!");
    }


    @ApiOperation(value = "修改学院", httpMethod = "POST")
    @RequestMapping("updataAcademyManager")
    public Result updataAcademyManager(Integer udId, String udName){
        Department department =new Department();
        department.setUdid(udId);
        department.setUdname(udName);
        int result=universityDepartmentMapper.updateById(department);
        if(result<=0){
            return Result.error("修改失败!");
        }
        return Result.success("修改成功");
    }

    @ApiOperation(value = "展示学院", httpMethod = "POST")
    @RequestMapping("showDepartment")
    public Result showDepartment(){
        try{
            List<Department> list = universityDepartmentMapper.showAll();
            return Result.success(list);
        }catch (Exception e){
            return Result.error("查询失败");
        }
    }

    @ApiOperation(value = "删除学院", httpMethod = "POST")
    @RequestMapping("deleteDepartment")
    public Result deleteDepartment(Long udId){
        int result=universityDepartmentMapper.deleteById(udId);
        if(result<=0){
            return Result.error("删除失败!");
        }
        return Result.success("删除成功");
    }

}
